home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / incl / basic.bak < prev    next >
Encoding:
Text File  |  1992-06-09  |  18.5 KB  |  716 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  basic.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef BASICH
  16. #define BASICH
  17.  
  18. #include <stream.h>
  19. #include <stdlib.h>
  20.  
  21. #ifndef __TURBOC__
  22.  
  23. #include <generic.h>
  24. #include <values.h>
  25.  
  26. #else
  27.  
  28. // TC values.h:
  29.  
  30. #define MAXINT          0x7FFF
  31. #define MAXLONG         0x7FFFFFFFL
  32. #define MAXFLOAT        3.37E+38
  33. #define MAXDOUBLE       1.797693E+308
  34.  
  35. // generic.h:
  36.  
  37. #define name2(z, y)             __name2(z, y)
  38. #define __name2(z, y)           z##y
  39. #define name3(z, y, x)          __name3(z, y, x)
  40. #define __name3(z, y, x)        z##y##x
  41. #define name4(z, y, x, w)       __name4(z, y, x, w)
  42. #define __name4(z, y, x, w)     z##y##x##w
  43.  
  44. #define declare(z, y) name2(z, declare)(y)
  45. #define implement(z, y) name2(z, implement)(y)
  46. #define declare2(z, y, x) name2(z, declare2)(y, x)
  47. #define implement2(z, y, x) name2(z, implement2)(y, x)
  48.  
  49. #endif   /* __TURBOC__ */
  50.  
  51.  
  52. //------------------------------------------------------------------------------
  53. // Global Type Definitions
  54. //------------------------------------------------------------------------------
  55.  
  56. typedef void* ent;
  57.  
  58.  
  59. typedef ent*  ent_ptr;
  60.  
  61. typedef int  (*CMP_ENT)(ent&,ent&);
  62. typedef ent  (*ENT_TO_ENT)(ent&);
  63. typedef int  (*ENT_TO_INT)(ent&);
  64. typedef void (*ENT_TO_VOID)(ent&);
  65.  
  66. typedef int  (*ANY_TO_INT)(...);
  67. typedef ent  (*ANY_TO_ENT)(...);
  68. typedef void (*ANY_TO_VOID)(...);
  69.  
  70.  
  71.  
  72.  
  73.  
  74. //------------------------------------------------------------------------------
  75. // Macros
  76. //------------------------------------------------------------------------------
  77.  
  78. #define Infinity MAXINT
  79.  
  80. #define nil NULL 
  81.  
  82. #ifdef __STDC__
  83. #define STRINGIZE(x) #x
  84. #else
  85. #define STRINGIZE(x) "x"
  86. #endif
  87.  
  88. #define name5(a,b,c,d,e)        name2(name2(a,b),name3(c,d,e))
  89.  
  90. #define declare1(a,t)           name2(a,declare1)(t)
  91. #define declare3(a,t1,t2,t3)    name2(a,declare3)(t1,t2,t3)
  92. #define declare4(a,t1,t2,t3,t4) name2(a,declare4)(t1,t2,t3,t4)
  93.  
  94. #define COMPARE(t,T) name3(t,T,_cmp) 
  95. #define APPLY(t,T)   name3(t,T,_apply) 
  96. #define ORD(t,T)     name3(t,T,_ord) 
  97.  
  98. #define Main            main(int argc, char** argv)
  99.  
  100. #define newline         (cout << "\n").flush()
  101.  
  102. #define forever         for(;;)
  103.  
  104. #define loop(a,b,c)     for (a=b;a<=c;a++)
  105.  
  106. #define Max(a,b)        ( (a>b) ? a : b )
  107.  
  108. #define Min(a,b)        ( (a>b) ? b : a )
  109.  
  110. #define SWAP(a,b)       if (a!=b) { *(int*)&a ^= int(b); \
  111.                     *(int*)&b ^= int(a); \
  112.                     *(int*)&a ^= int(b); }
  113.  
  114.  
  115. // iteration macros for lists and sets:
  116.  
  117. #define forall(x,S)       for((S).init_iterator(); (S).next_element(x); )
  118. #define forall_items(x,S) for(x = (S).first_item(); x; x = (S).next_item(x) )
  119.  
  120.  
  121. //------------------------------------------------------------------------------
  122. // compare, Print, Read, Init, Copy, Clear, Ent 
  123. // for basic builtin types
  124. //------------------------------------------------------------------------------
  125.  
  126. //overloaded compare: used to define linear orders in virtual cmp functions 
  127.  
  128. inline int compare(char& x, char& y)    { return x-y; }
  129. inline int compare(int&  x, int&  y)    { return x-y; }
  130. inline int compare(void* x, void* y)    { return int(x)-int(y); }
  131.  
  132. extern int compare(double&, double&);
  133.  
  134. //overloaded Print: used to define virtual print functions 
  135.  
  136. inline void Print(char& x, ostream& out = cout)  { out << x; }
  137. inline void Print(int&  x, ostream& out = cout)  { out << x; }
  138. inline void Print(void* x, ostream& out = cout)  { out << "0x" << hex(int(x)); }
  139.  
  140.  
  141. //overloaded Read: used to define virtual read functions 
  142.  
  143.  
  144. inline void Read(char& x, istream& in = cin)   { in >> x; }
  145. inline void Read(int&  x, istream& in = cin)   { in >> x; }
  146. inline void Read(void*, istream&)   {}
  147.  
  148.  
  149. //overloaded Init: used to define virtual initialization functions 
  150.  
  151. inline ent Init(char& x) { x=0; return ent(x); }
  152. inline ent Init(int&  x) { x=0; return ent(x); }
  153. inline ent Init(void* x) { x=0; return ent(x); }
  154.  
  155.  
  156. //overloaded Clear: used to define virtual clear functions
  157.  
  158. inline void Clear(char& x) { x=0; }
  159. inline void Clear(int&  x) { x=0; }
  160. inline void Clear(void* x) { x=x; }
  161.  
  162. //overloaded Copy: used to define virtual copy functions
  163.  
  164. inline ent Copy(const char& x)  { return ent(x); }
  165. inline ent Copy(const int&  x)  { return ent(x); }
  166. inline ent Copy(void* x)        { return ent(x); }
  167.  
  168.  
  169. //overloaded Ent:  used for converting pointer types to void* 
  170.  
  171. inline ent Ent(void* x) { return (void*)(x); }
  172. inline ent Ent(int   x) { return (void*)(x); }
  173.  
  174.  
  175. //------------------------------------------------------------------------------
  176. // Memory Management
  177. //------------------------------------------------------------------------------
  178.  
  179. struct  memory_elem_type { memory_elem_type* next; };
  180.  
  181. typedef memory_elem_type* memory_elem_ptr;
  182.  
  183.  
  184. extern const int memory_word_size;   // size of word   (in bytes) 4
  185. extern const int memory_block_bytes; // size of block  (in bytes) 2^k - 4
  186. extern const int memory_block_words; // size of block  (in words)
  187. extern const int memory_max_size;    // maximal size of a structure (words)
  188.  
  189. extern memory_elem_ptr memory_free_list[];
  190. extern long int        memory_total_count[];
  191.  
  192. extern memory_elem_ptr memory_block_list;
  193. extern int             memory_block_count;
  194.  
  195. extern void            memory_free_blocks();
  196. extern memory_elem_ptr memory_allocate_block(int);
  197. extern memory_elem_ptr allocate_words(int size);
  198. extern memory_elem_ptr Allocate_words(int size);
  199. extern void            deallocate_words(void* p, int size);
  200. extern void            Deallocate_words(void* p, int size);
  201. extern void            deallocate_list(void* head,void* tail, int size);
  202.  
  203. extern void            print_statistics();
  204.  
  205. inline memory_elem_ptr allocate_bytes(int n)
  206. { int s = n/memory_word_size;
  207.   if (n%memory_word_size) s++;
  208.   return allocate_words(s);
  209. }
  210.  
  211. inline void deallocate_bytes(void* p, int n)
  212. { int s = n/memory_word_size;
  213.   if (n%memory_word_size) s++;
  214.   deallocate_words(p,s);
  215. }
  216.  
  217.  
  218. #define OPERATOR_NEW(size)\
  219. void* operator new(size_t)\
  220. { memory_elem_ptr p = memory_free_list[size];\
  221.   if (p==0) p = memory_allocate_block(size);\
  222.   memory_free_list[size] = p->next;\
  223.   return p;\
  224.  }
  225.  
  226. #define OPERATOR_DEL(size)\
  227. void  operator delete(void* p)\
  228. { memory_elem_ptr(p)->next = memory_free_list[size];\
  229.   memory_free_list[size] = memory_elem_ptr(p);\
  230. }
  231.  
  232. //------------------------------------------------------------------------------
  233. // Basic Data Types
  234. //------------------------------------------------------------------------------
  235.  
  236. enum rel_pos { before = 1, after = 0 };
  237.  
  238. enum direction { forward = 0, backward = 1 };
  239.  
  240. //------------------------------------------------------------------------------
  241. // boolean
  242. //------------------------------------------------------------------------------
  243.  
  244.  
  245. /*
  246. enum bool { false = 0, true = 1 };
  247. */
  248.  
  249.  
  250. typedef int bool;
  251.  
  252. extern const bool true;
  253. extern const bool false;
  254.  
  255.  
  256.  
  257. //------------------------------------------------------------------------------
  258. // real numbers
  259. //------------------------------------------------------------------------------
  260.  
  261. class real{
  262.  
  263. struct rrep { 
  264.    
  265.       double d;     
  266.       int    n;     // number of references
  267.  
  268.  rrep(double x=0); 
  269.  
  270. OPERATOR_NEW(3)
  271. OPERATOR_DEL(3)
  272.  
  273. };
  274.  
  275. rrep* p;     
  276.  
  277. public:
  278.  
  279. real()               { p = new rrep; }
  280. real(const double d) { p = new rrep(d); }
  281. real(const int d)    { p = new rrep(d); }
  282. real(const real& r)  { p = r.p; p->n++; }
  283. real(ent x)          { p=(rrep*)x; p->n++; }
  284. void clear()         { if (--p->n == 0) delete p; }
  285.  
  286. ~real()              { clear(); }
  287.  
  288.  
  289. double operator~() const { return p->d; }
  290.  
  291. double* dp()       { return &(p->d); }
  292.  
  293. int round()        const { return (p->d>0) ? int(p->d+0.5) : int(p->d-0.5); }
  294. int trunc()        const { return int(p->d); }
  295.  
  296. operator double() const { return p->d; }  
  297.  
  298. real  operator-() const { return -(p->d); }
  299.  
  300. real& operator=(const double x);
  301. real& operator=(const real& x);
  302.  
  303. real& operator+=(const real& r)  { *this = p->d + r.p->d; return *this ; }
  304. real& operator+=(double d)       { *this = p->d + d;      return *this ; }
  305.  
  306. real& operator-=(const real& r)  { *this = p->d - r.p->d; return *this ; }
  307. real& operator-=(double d)       { *this = p->d - d;      return *this ; }
  308.  
  309. real& operator*=(const real& r)  { *this = p->d * r.p->d; return *this ; }
  310. real& operator*=(double d)       { *this = p->d * d;      return *this ; }
  311.  
  312. real& operator/=(const real& r)  { *this = p->d / r.p->d; return *this ; }
  313. real& operator/=(double d)       { *this = p->d / d;      return *this ; }
  314.  
  315.  
  316.  
  317.  
  318. friend ostream& operator<<(ostream&, const real&);
  319. friend istream& operator>>(istream&, real&); 
  320.  
  321. friend int  compare(real&, real&);
  322.  
  323. friend ent  Init(real& x)        { x.p->n++; return ent(x.p); }
  324. friend ent  Copy(const real& x)  { x.p->n++; return ent(x.p); }
  325. friend void Clear(real& x)       { x.clear(); }
  326. friend void Print(real& x, ostream& out = cout) { out << x; }
  327. friend void Read(real& x, istream& in = cin)    { in  >> x; }
  328.  
  329. friend ent  Ent(const real& x)    { return ent(x.p); }
  330.  
  331. };
  332.  
  333. //------------------------------------------------------------------------------
  334. // REAL(cmp): reals with user defined linear order cmp
  335. //------------------------------------------------------------------------------
  336.  
  337. #define REAL(cmp) name2(real_,cmp)
  338.  
  339. #define REALdeclare(cmp)\
  340. struct REAL(cmp) : public real \
  341. {  REAL(cmp)(ent p)        : real(p) {}\
  342.    REAL(cmp)(real  r )     : real(r) {}\
  343.    REAL(cmp)(REAL(cmp)& r) : real(r) {}\
  344.    REAL(cmp)() {}\
  345.   ~REAL(cmp)() {}\
  346. };\
  347. \
  348. int compare(REAL(cmp)& x, REAL(cmp)& y) { return cmp(x,y); }
  349.  
  350.  
  351. //------------------------------------------------------------------------------
  352. // INT(cmp): int with user defined linear order cmp
  353. //------------------------------------------------------------------------------
  354.  
  355. #define INT(cmp) name2(int_,cmp)
  356.  
  357. #define INTdeclare(cmp)\
  358. struct INT(cmp)\
  359. {  int p;\
  360.  INT(cmp)()                  { p = 0; }\
  361.  INT(cmp)(const int i)       { p = i;}\
  362.  INT(cmp)(ent x)             { p = (int)x; }\
  363.  operator int() { return p; }\
  364. \
  365. friend ent  Init(INT(cmp)&  x) { x.p = 0; return ent(x.p);}\
  366. friend ent  Copy(const INT(cmp)&  x) { return ent(x.p); }\
  367. friend void Clear(INT(cmp)& x) { x.p = 0; }\
  368. friend void Print(INT(cmp)& x, ostream& out = cout){ out << x.p; }\
  369. friend void Read(INT(cmp)&  x, istream& in = cin)  { in  >> x.p; }\
  370. friend ent  Ent(const INT(cmp)& x)                 { return ent(x.p); }\
  371. \
  372. };\
  373. \
  374. int compare(INT(cmp)& x, INT(cmp)& y) { return cmp(x,y); }
  375.  
  376.  
  377. //------------------------------------------------------------------------------
  378. // strings
  379. //------------------------------------------------------------------------------
  380.  
  381. extern char* str_dup(const char*);
  382. extern char* str_cat(const char*,const char*);
  383.  
  384. class string {
  385.  
  386. struct srep { 
  387.    
  388.       char* s;       // pointer to c-string
  389.       int   n;       // number of references
  390.  
  391. srep(char* p) { s = p; n = 1; }
  392.  
  393. OPERATOR_NEW(2)
  394. OPERATOR_DEL(2)
  395.  
  396. };
  397.  
  398.       srep* p;
  399.  
  400. public:
  401.  
  402.  
  403.   string()                { p = new srep(str_dup(""));}
  404.   string(const string& x) { x.p->n++; p = x.p; }
  405.   string(ent x)           { p=(srep*)x; p->n++; }
  406.   string(int  )           { p = new srep(str_dup("")); }
  407.  
  408.  string(const char*, ...);  // form-like constructor
  409.  
  410. #if defined(__GNUG__) || defined(__TURBOC__)
  411.  
  412. cfront: ambiguity  string(const char*, ...)  <--> string(const char*)
  413.  
  414. string(const char* s)   { PTR = new srep(s);}
  415.  
  416. #endif
  417.  
  418.  
  419.  void clear()       { if (--p->n == 0) { delete p->s; delete p; } }
  420.  
  421. ~string()           { clear(); }
  422.  
  423.  
  424. char*  operator~()  const   { return str_dup(p->s); }
  425. char*  cstring()    const   { return p->s; }
  426.  
  427. int    length()          const;
  428. string sub(int,int)      const;
  429. int    pos(string,int=0) const;
  430.  
  431. string operator()(int i, int j)  const { return sub(i,j); }
  432. string head(int i)               const { return sub(0,i-1); }
  433. string tail(int i)               const { return sub(length()-i,length()-1); }
  434.  
  435. string insert(string, int=0)             const;
  436. string insert(int, string)               const;
  437.  
  438. string replace(string, string, int=1)    const; 
  439. string replace(int, int, string)    const; 
  440.  
  441. string del(string, int=1)    const; 
  442. string del(int, int)         const; 
  443.  
  444. void   read(istream&, char=' ');
  445. void   read(char delim=' ')     { read(cin,delim); }
  446.  
  447. string replace_all(string s1, string s2) const  { return replace(s1,s2,0); } 
  448. string replace(int i, string s)          const  { return replace(i,i,s);  } 
  449.  
  450. string del_all(string s)                 const  { return del(s,0); }
  451. string del(int i)                        const  { return del(i,i); }  
  452.  
  453. void   read_line(istream& I = cin)              { read(I,'\n'); }
  454.  
  455. string format(string) const;
  456.  
  457. string& operator=(const char*);
  458. string& operator=(const string&);
  459.  
  460. char  operator[](int) const;
  461. char& operator[](int);
  462.  
  463. string operator+(const string& x)  const;
  464. string operator+(const char* s1)   const;
  465. string& operator+=(const char* s1);
  466. string& operator+=(const string& x);
  467.  
  468.  
  469. friend int operator==(const string& x, const char* s)   ;
  470. friend int operator==(const string& x, const string& y) ;
  471. friend int operator!=(const string& x, const char* s)   ;
  472. friend int operator!=(const string& x, const string& y) ;
  473. friend int operator<(const string& x, const string& y)  ;
  474. friend int operator>(const string& x, const string& y)  ;
  475. friend int operator<=(const string& x, const string& y) ;
  476. friend int operator>=(const string& x, const string& y) ;
  477.  
  478.  
  479. friend istream& operator>>(istream&, string&); 
  480. friend ostream& operator<<(ostream&, const string&) ;
  481.  
  482. friend int  compare(string& x, string& y);
  483.  
  484. friend ent  Init(string&  x)       { x.p->n++; return ent(x.p); }
  485. friend ent  Copy(const string&  x) { x.p->n++; return ent(x.p); }
  486. friend void Clear(string& x)       { x.clear(); }
  487. friend void Print(string& x, ostream& out = cout) { out << x; }
  488. friend void Read(string&  x, istream& in = cin)   { in  >> x; }
  489.  
  490. friend ent  Ent(const string& x)    { return ent(x.p); }
  491.  
  492. };
  493.  
  494. //------------------------------------------------------------------------------
  495. // STRING(cmp): string with user defined linear order cmp
  496. //------------------------------------------------------------------------------
  497.  
  498. #define STRING(cmp) name2(string_,cmp)
  499.  
  500. #define STRINGdeclare(cmp)\
  501. struct STRING(cmp) : public string \
  502. {  STRING(cmp)(ent p)          : string(p) {}\
  503.    STRING(cmp)(string s)       : string(s) {}\
  504.    STRING(cmp)(STRING(cmp)& s) : string(s) {}\
  505.    STRING(cmp)() {}\
  506.  ~ STRING(cmp)() {}\
  507. };\
  508. \
  509. int compare(STRING(cmp)& x, STRING(cmp)& y) { return cmp(x,y); }
  510.  
  511.  
  512. //------------------------------------------------------------------------------
  513. // some useful functions
  514. //------------------------------------------------------------------------------
  515.  
  516. typedef int (*SIG_PF) (...);
  517.  
  518. extern SIG_PF signal(int, SIG_PF);
  519.  
  520. extern int interrupt_handler(...);
  521.  
  522. inline SIG_PF catch_interrupts(SIG_PF handler = interrupt_handler)
  523. { return signal(2,handler); }
  524.  
  525.  
  526. #ifdef __TURBOC__
  527. inline void srandom(int seed) { srand(seed);   }
  528. inline int  random()          { return rand(); }
  529. #endif
  530.  
  531.  
  532. extern void    init_random(int=0);
  533. extern double  rrandom();                   // real random number in ]0..1[
  534.  
  535. inline unsigned random(unsigned a, unsigned b)  
  536.                   { return a + unsigned(random())%(b-a+1); }
  537.  
  538. inline int random(int a, int b)   { return a + random()%(b-a+1); }
  539.  
  540.  
  541.  
  542. extern double used_time();
  543. extern double used_time(real&);
  544. extern double used_time(double&);
  545. extern float  used_time(float&);
  546. extern void   print_time(string s);
  547.  
  548. inline void print_time() { print_time(""); }
  549.  
  550.  
  551.  
  552. //------------------------------------------------------------------------------
  553. // input/output
  554. //------------------------------------------------------------------------------
  555.  
  556. // Problems with cfront's stdarg on SPARCs
  557.  
  558. #define Va_Start  save_registers(); va_start
  559.  
  560. extern "C" void   save_registers(); // cfront&sparc: saves registers on stack
  561.                     // others: do nothing
  562.  
  563.  
  564. // if not GNU replace form calls by string constructor
  565.  
  566. #ifndef __GNUG__
  567. #define form ~string
  568. #endif
  569.  
  570.  
  571.  
  572. class file_ostream : public ostream {
  573.  
  574. filebuf fb;
  575.  
  576. public:
  577.  
  578. file_ostream() : ostream(&fb) {}
  579. file_ostream(string);
  580.  
  581. bool open(string);
  582. void close();
  583.  
  584. ~file_ostream() { close(); }
  585.  
  586. };
  587.  
  588.  
  589. class file_istream : public istream {
  590.  
  591. filebuf fb;
  592.  
  593. public:
  594.  
  595. file_istream() : istream(&fb) {}
  596. file_istream(string);
  597.  
  598. bool open(string);
  599. void close();
  600.  
  601. ~file_istream() { close(); }
  602.  
  603. };
  604.  
  605.  
  606.  
  607.  
  608. struct string_istream : public istream { 
  609.  
  610. char buf[2048];
  611.  
  612. public:
  613.  
  614. string_istream(string);
  615. string_istream(char*);
  616. string_istream(int, char**);
  617.  
  618. };
  619.  
  620.  
  621. class string_ostream : public ostream { 
  622.  
  623. char buf[2048];
  624.  
  625. public:
  626.  
  627. string_ostream();
  628. string str();
  629.  
  630. };
  631.  
  632.  
  633.  
  634. #ifndef __TURBOC__
  635.  
  636. struct cmd_ostream : public ostream {
  637.  
  638. #ifndef __GNUG__
  639.     stdiobuf b;
  640. #endif
  641. cmd_ostream(string);
  642.  
  643. };
  644.  
  645.  
  646. struct cmd_istream : public istream {
  647.  
  648. #ifndef __GNUG__
  649.     stdiobuf b;
  650. #endif
  651.  
  652. cmd_istream(string);
  653.  
  654. };
  655.  
  656. #endif
  657.  
  658.  
  659.  
  660.  
  661. extern int     Yes(string s);
  662. extern int     Yes();
  663.  
  664. extern int     read_int(string s);
  665. extern int     read_int();
  666.  
  667. extern char    read_char(string s);
  668. extern char    read_char();
  669.  
  670. extern double  read_real(string s);
  671. extern double  read_real();
  672.  
  673. extern string  read_string(string s);
  674. extern string  read_string();
  675.  
  676. extern string  read_line(istream& s);
  677. extern string  read_line();
  678.  
  679.  
  680. //------------------------------------------------------------------------------
  681. // Error Handling
  682. //------------------------------------------------------------------------------
  683.  
  684. typedef void (*PEH)(int,char*);   // Pointer to Error Handler
  685.  
  686. extern PEH p_error_handler;
  687. extern PEH set_error_handler(PEH);
  688. extern void default_error_handler(int,char*);
  689.  
  690.  
  691. inline void error_handler(int i, char* s)  { p_error_handler(i,s); }
  692. inline void error_handler(int i, string s) { p_error_handler(i,~s); }
  693.  
  694.  
  695. //------------------------------------------------------------------------------
  696. // LEDA INIT
  697. //------------------------------------------------------------------------------
  698.  
  699. extern const char* leda_version_string;
  700.  
  701. class LEDA_INIT {
  702.  
  703. char* init_list;
  704.  
  705. public:
  706.  
  707.  LEDA_INIT();
  708. ~LEDA_INIT();
  709.  
  710. };
  711.  
  712. extern LEDA_INIT LEDA;
  713.  
  714. #endif BASICH
  715.